package com.arm.service.impl;

import com.arm.dao.RoleMapper;
import com.arm.dao.RoleMenuRelativeMapper;
import com.arm.module.AdminMenu;
import com.arm.module.Role;
import com.arm.module.RoleMenuRelative;
import com.arm.module.admin.Menu;
import com.arm.service.BaseService;
import com.arm.service.MenuService;
import com.arm.service.RoleService;
import com.arm.tools.myRunTime.MyRunTimeExcption;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created by Baggio on 2017/3/12.
 */
@Service
public class RoleServiceImpl extends BaseService implements RoleService {
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    RoleMenuRelativeMapper roleMenuRelativeMapper;
@Autowired
    MenuService menuService;

    @Override
    public List<Role> getRoleList(Map<String, String> map) {

        List<Role> roleList=roleMapper.selectRoleList(map);

        return roleList;



    }

    private void insertRole(Role role) {
        roleMapper.insertRole(role);
    }

    private void updateRole(Role role) {
        roleMapper.updateRoleById(role);
    }

    @Transactional
    @Override
    public void modifyRole(Role role, List<Long> menuIdList) {
        Long tid = role.getTid();

        if (tid == null) {
            role.setCreateTime(new Date());
            insertRole(role);

        } else {
            if (StringUtils.isBlank(role.getRoleName())) {
                throw new MyRunTimeExcption("角色名不能为空！");
            }
            roleMenuRelativeMapper.deleteRoleMenuRelative(role.getTid());
            updateRole(role);
        }
        if (menuIdList != null && menuIdList.size() > 0) {


            for (Long menuId : menuIdList) {
                RoleMenuRelative roleMenuRelative = new RoleMenuRelative();
                roleMenuRelative.setRoleId(role.getTid());
                if (menuId != null) {
                    roleMenuRelative.setMenuId(menuId);
                }
                roleMenuRelativeMapper.insertRoleMenuRelative(roleMenuRelative);
            }

        }


//
//        String menuIds = map.get("menuIds");
//        String menuIdAry[] = menuIds.split(",");
//        Role role = roleMapper.selectRole(map);
//        roleMenuRelativeMapper.deleteRoleMenuRelative(role.getTid());
//        for (String menuId : menuIdAry) {
//            RoleMenuRelative roleMenuRelative = new RoleMenuRelative();
//            roleMenuRelative.setRoleId(role.getTid());
//            if (menuId != "") {
//                roleMenuRelative.setMenuId(Integer.parseInt(menuId));
//            }
//            roleMenuRelativeMapper.insertRoleMenuRelative(roleMenuRelative);
//        }
//        role.setRoleName(roleName);
//        roleMapper.updateRoleById(role);
    }

    @Override
    public void addRole(Map<String, String> map) {
        Role role = new Role();
        System.out.println(map.get("tid") + ".................");
        if (map.get("tid") == "") {
            String roleName = map.get("roleName");
            if (StringUtils.isBlank(roleName)) {
                throw new MyRunTimeExcption("roleName is null");
            }
            role.setRoleName(roleName);
            role.setCreateTime(new Date());
            roleMapper.insertRole(role);
        } else {
            Long tid = Long.parseLong(map.get("tid"));
            String roleName = map.get("roleName");
            if (StringUtils.isBlank(roleName)) {
                throw new MyRunTimeExcption("roleName is null");
            }
            role.setTid(tid);
            role.setRoleName(roleName);
            roleMapper.updateRoleById(role);
        }
    }

    @Override
    public Role selectRole(Map<String, String> map) {
        return roleMapper.selectRole(map);
    }
}
